package light.mvc.service.admin.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.admin.Tdepartment;
import light.mvc.model.admin.Tuser;
import light.mvc.page.admin.Department;
import light.mvc.page.base.DropDownBox;
import light.mvc.page.base.PageFilter;
import light.mvc.page.base.Tree;
import light.mvc.service.admin.DepartmentServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DepartmentServiceImpl implements DepartmentServiceI {

	@Autowired
	private BaseDaoI<Tdepartment> departmentDao;

	@Override
	public void add(Department department) {
		Tdepartment t = new Tdepartment();
		BeanUtils.copyProperties(department, t);
		departmentDao.save(t);	
	}

	@Override
	public void delete(Integer id) {
		Tdepartment t = departmentDao.get(Tdepartment.class, id);
		departmentDao.delete(t);
	}

	@Override
	public void edit(Department department) {
		Tdepartment t = departmentDao.get(Tdepartment.class,department.getId());
		BeanUtils.copyProperties(department, t);
		departmentDao.update(t);
	}

	@Override
	public Department get(Integer id) {
		Tdepartment t = departmentDao.get(Tdepartment.class,id);
		Department department = new Department();
		BeanUtils.copyProperties(t, department);
		
		return department;
	}
	
	@Override
	public List<Department> dataGrid(Department departmentSome, PageFilter ph) {
		List<Department> departments = new ArrayList<Department>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Tdepartment t ";
		List<Tdepartment> list = departmentDao.find(hql + whereHql(departmentSome, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (Tdepartment t : list) {
			Department department = new Department();
			BeanUtils.copyProperties(t, department);
			departments.add(department);
		}
		return departments;
	}
	
	@Override
	public Long count(Department department, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Tdepartment t ";
		return departmentDao.count("select count(*) " + hql + whereHql(department, params), params);
	}
	
	private String whereHql(Department department, Map<String, Object> params) {
		String hql = "";
		if (department != null) {
			hql += " where 1=1 ";
			if (department.getName()!= null) {
				hql += " and t.name like :name";
				params.put("name", "%" + department.getName() + "%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	@Override
	public Department getByName(String name) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("name", name);
		Tdepartment t = departmentDao.get("from Tdepartment t  where t.name = :name",params);
		Department department = new Department();
		if(t!=null){
			BeanUtils.copyProperties(t, department);
		}else{
			return null;
		}
		return department;
	}
	
	@Override
	public List<DropDownBox> listName() {
		List<DropDownBox> dropDownList = new ArrayList<DropDownBox>();
		List<Tdepartment> list = departmentDao.find("select t from Tdepartment t order by t.name");
		if ((list != null) && (list.size() > 0)) {
			for (Tdepartment t : list) {
				DropDownBox box = new DropDownBox();
				box.setId(t.getId().toString());
				box.setText(t.getName());
				dropDownList.add(box);
			}
		}
		return dropDownList;
	}
	
	@Override
	public List<Tree> listDoctors() {
		List<Tdepartment> list = null;
		List<Tree> trees = new ArrayList<Tree>();

		list = departmentDao.find("select t from Tdepartment t order by t.name");

		if ((list != null) && (list.size() > 0)) {
			for (Tdepartment department : list) {
				Tree tree = new Tree();
				tree.setId("department" + department.getId().toString());
				tree.setText(department.getName());
				tree.setIconCls("icon_folder");
				
				Set<Tuser> doctors = department.getUsers();
				for(Tuser doctor: doctors) {
					Tree innerTree = new Tree();
					innerTree.setId("doctor" +doctor.getId().toString());
					innerTree.setText(doctor.getName());
					innerTree.setPid("department" + department.getId().toString());
					innerTree.setIconCls("icon_resource");
					trees.add(innerTree);
				}
				
				trees.add(tree);
			}
		}
		return trees;
	}
}
